﻿@using KL.Store.Core
@{
    ViewBag.Title = "Stok İşlemleri";
    Layout = "~/Views/Shared/_LayoutReportPanel.cshtml";
}
<div id="processResult"></div>
<br />
<div class="row">
    <div class="col-md-6">
        <div class="input-group2">
            <input type="text" name="searchProductPanel" id="searchProductPanel" class="form-control" placeholder="Aranacak terimi yazın...">
            <div id="productsearch_search_resultsPanel" class="window productsearchviewPanel" style="display: none;">
                <div id="divProductSearchResultPanel" style="padding: 6px;">
                </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xs-12" id="productSearchResult">
    </div>
</div>
<script>
    $('.editLiveSearch').live('click', function ()
    {
        $.fancybox.open({
            href: $(this).attr('href'),
            autoScale: true,
            width: 1000,
            height: 650,
            fitToView: true,
            autoSize: false,
            autoDimensions: true,
            autoScale: false,
            type: 'iframe',
            afterClose: function ()
            {
            }
        });
        return false;
    });
    function GetSearchProductPanel()
    {
        var myText = $("#searchProductPanel").val();
        $.ajax({
            url: '/Search/LiveSearchFull?search=' + myText,
            type: 'POST',
            success: function (result)
            {
                $("#divLoadingGif").css("display", "none");
                var myProductList = $(result);
                var myHtlmString = '';
                $.each(myProductList[0].LiveSearch, function (key, val)
                {
                    myHtlmString += '<li><a href="/ProductManage/Edit/' + val.ProductId + '" data-id="' + val.ProductId + '" class="editLiveSearch">' + val.ProductId + ' - ' + val.Barcode + ' - ' + val.ProductName + '</a></li>';
                });
                myHtlmString = '<ul id="live_productsearch_resultsPanel">' + myHtlmString + '</ul><div class="clearfix"></div>';
                $("#divProductSearchResultPanel").html(myHtlmString);

                //$('.editLiveSearch').fancybox({
                //    autoScale: true,
                //    width: 1000,
                //    height: 650,
                //    fitToView: true,
                //    autoSize: false,
                //    autoDimensions: true,
                //    autoScale: false,
                //    afterClose: function ()
                //    {
                //    }
                //});
            }
        });
    }
    var searchPanel = (function ()
    {
        var builderPanel = function ()
        {
            $(document).ready(function ()
            {
                var lastSearched = "";
                var object = $('#searchProductPanel', $('.window.productsearchviewPanel').hide().parent()).focus(function ()
                {
                    this.active = true;
                    var value = $.trim(this.value);
                    if (value == object.data('empty'))
                    {
                        object.val('').parent().addClass('focus');
                    } else if (value.length >= 3) { searchPanel.show(); }
                }).blur(function ()
                {
                    this.active = false;

                    $(this.parentNode).removeClass('focus');
                    if ($.trim(object.val()) == '') { object.val(object.data('empty')); }
                }).keyup(function (e)
                {
                    if (e.which != 38 && e.which != 40 && e.which != 13 && e.which != 27 && lastSearched != $('#searchProductPanel').val())
                    {
                        lastSearched = $('#searchProductPanel').val();

                        $("#divLoadingGif").css("display", "block");
                        GetSearchProductPanel();
                        searchPanel.show();
                        $(this.parentNode).removeClass('focus');

                    }
                });


                var parent = object.parents('.container').hover(function () { this.active = true; }, function ()
                {
                    this.active = false;
                    if (!object.prop('active')) { searchPanel.hide(); }
                });
            });
        }
        builderPanel.prototype = {
            busy: false,
            show: function () { $('.window.productsearchviewPanel').stop(true, true).animate({ opacity: 'show', height: 'show' }, 'fast'); },
            hide: function () { $('.window.productsearchviewPanel').stop(true, true).animate({ opacity: 'hide', height: 'hide' }, 'fast'); }
        };

        return new builderPanel();
    })();
    var headerElement = $('.input-group2');
    headerElement.find('input#searchProductPanel').keyup(function (e)
    {
        var ls = headerElement.find('#productsearch_search_resultsPanel');
        var lc = headerElement.find('#productsearch_search_resultsPanel #divProductSearchResultPanel #live_productsearch_resultsPanel li').size();
        if (lc > 0)
        {
            var newSelected = null;
            selectedSize = headerElement.find('#productsearch_search_resultsPanel li a.selected').size();
            selectedItem = headerElement.find('#productsearch_search_resultsPanel li a.selected');
            if (e.which == 40)
            {
                //down arrow key
                if (selectedSize <= 0)
                {
                    headerElement.find('#productsearch_search_resultsPanel li:first-child a').addClass('selected');
                }
                else
                {
                    newSelected = headerElement.find('#productsearch_search_resultsPanel li a.selected').parent().next().find('a');
                    headerElement.find('#productsearch_search_resultsPanel li a').removeClass('selected');
                    newSelected.addClass('selected');
                }
                return false;
            }
            else if (e.which == 38)
            {
                //up arrow key
                if (selectedSize <= 0)
                {
                    headerElement.find('#productsearch_search_resultsPanel li:last-child a').addClass('selected');
                }
                else
                {
                    newSelected = headerElement.find('#productsearch_search_resultsPanel li a.selected').parent().prev().find('a');
                    headerElement.find('#productsearch_search_resultsPanel li a').removeClass('selected');
                    newSelected.addClass('selected');
                }
                return false;
            }
            else if (e.which == 13)
            {
                //enter key
                if (selectedSize <= 0)
                {
                    loadingMsg();
                    searchPanel.hide();
                    $('#productSearchResult').load('/ReportPanel/StokArama', { Search: headerElement.find('#searchProductPanel').val() }, function ()
                    {
                        $('.loadingMsg').remove();
                    });
                }
                else
                {
                    selectedItem.click();
                }
            }
            else if (e.which == 27)
            {
                searchPanel.hide();
            }
        }
    });
</script>
